local skel = fk.CreateSkill {
  name = "lb__sanwu",
}

Fk:loadTranslationTable {
  ["lb__sanwu"] = "三无",
  [":lb__sanwu"] = "你可以将一张非锦囊牌、未使用过的牌或不能选择目标的牌当【杀】使用或打出，若皆满足则此牌无次数限制、无距离限制、无视防具牌。",

  ["#lb__sanwu"] = "三无：你可以将一张非锦囊牌、未使用过的牌或不能选择目标的牌当【杀】使用或打出",
  ["#lb__sanwu-ask"] = "三无：你可以将 %arg 当【杀】使用或打出",
  ["#lb__sanwu-slash"] = "三无：你可以将 %arg 当【杀】使用或打出，此牌无次数限制、无距离限制、无视防具牌",
  ["@$lb__sanwu"] = "三无",
  ["lb__sanwu-slash"] = "三无",

  ["$lb__sanwu1"] = "不避，不悔。",
  ["$lb__sanwu2"] = "无惧，无畏。",
  ["$lb__sanwu3"] = "我将，巡征追猎。",
  ["$lb__sanwu4"] = "天击之责，至死方休。",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = function(self, player, selected_cards, selected)
    local card = Fk:getCardById(selected_cards[1])
    if card then
      local prompt = "#lb__sanwu-ask"
      if card.type ~= Card.TypeTrick and
          not table.contains(player:getTableMark("@$lb__sanwu"), card.trueName) and
          (card:getFixedTargets(player) or card.is_passive) then
        prompt = "#lb__sanwu-slash"
      end
      return prompt .. ":::" .. card:toLogString()
    end
    return "#lb__sanwu"
  end,
  card_filter = function(self, player, to_select, selected, selected_targets)
    local card = Fk:getCardById(to_select)
    if #selected == 0 then
      local num = 0
      if card.type ~= Card.TypeTrick then num = num + 1 end
      if not table.contains(player:getTableMark("@$lb__sanwu"), card.trueName) then num = num + 1 end
      if (card:getFixedTargets(player) or card.is_passive) then num = num + 1 end
      if num == 3 then return true end
      local slash = Fk:cloneCard("slash")
      slash.skillName = skel.name
      slash:addSubcard(to_select)
      return player:canUseOrResponseInCurrent(slash)
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return nil end
    local skillName = skel.name
    local card = Fk:getCardById(cards[1])
    if card.type ~= Card.TypeTrick and
        not table.contains(player:getTableMark("@$lb__sanwu"), card.trueName) and
        (card:getFixedTargets(player) or card.is_passive) then
      skillName = "lb__sanwu-slash"
    end
    local slash = Fk:cloneCard("slash")
    slash.skillName = skillName
    slash:addSubcards(cards)
    return slash
  end,
  before_use = function(self, player, use)
    if table.contains(use.card.skillNames, "lb__sanwu-slash") then
      --use.unoffsetableList = player.room.players
      use.extra_data = use.extra_data or {}
      use.extra_data.lb__sanwu = player.id
    end
  end,
})

skel:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    return (data.extra_data or {}).lb__sanwu == player.id
  end,
  on_refresh = function(self, event, target, player, data)
    data.to:addQinggangTag(data)
  end,
})

skel:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    if card then
      if table.contains(card.skillNames, "lb__sanwu-slash") then
        return true
      elseif table.contains(card.skillNames, "lb__sanwu") and #card.subcards ~= 1 then
        return true
      end
    end
    --return card and (table.contains(card.skillNames, "lb__sanwu-slash") or table.contains(card.skillNames, "lb__sanwu"))
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and table.contains(card.skillNames, "lb__sanwu-slash")
  end,
})


skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, "@$lb__sanwu", data.card.trueName)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local list = {}
  player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
    local use = e.data
    if use.from == player then
      table.insertIfNeed(list, use.card.trueName)
    end
  end, 0)
  player.room:setPlayerMark(player, "@$lb__sanwu", #list == 0 and 0 or list)
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@$lb__sanwu", 0)
end)

return skel
